import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

import { ReactKeycloakProvider } from '@react-keycloak/web'
import keyCloak from './keycloak'


const eventLogger = (event: unknown, error: unknown) => {
  
  switch (event) {
    case "onAuthSuccess":
      keyCloak.loadUserProfile().then((profile)=>{
        console.log("onAuthSuccess", profile);
      });
      keyCloak.loadUserInfo().then((userInfo)=>{
        console.log("onAuthSuccess",userInfo,keyCloak.userInfo);
      });
      break;
    case "onTokenExpired":
        console.log("onTokenExpired");
        keyCloak.clearToken();
        keyCloak.logout();
      break;
    default:
      console.log('onKeycloakEvent', event, typeof event, error)
      break;
  }

// onReady
// onInitError
// onAuthSuccess
// onAuthError
// onAuthRefreshSuccess
// onAuthRefreshError
// onTokenExpired
// onAuthLogout

}

const tokenLogger = (tokens: unknown) => {
  console.log('onKeycloakTokens', tokens)
}

// const loadUserProfile = (tokens: unknown) => {
//   console.log('onKeycloakTokens', tokens)
// }

const initOptions = {
  onLoad: 'login-required'
}
// const initOptions = {
//   onLoad: 'check-sso'
// }

ReactDOM.render(
  <ReactKeycloakProvider authClient={keyCloak} initOptions={initOptions} onEvent={eventLogger} 
    onTokens={tokenLogger} >
    <React.StrictMode>
      <App />
    </React.StrictMode>
  </ReactKeycloakProvider>, document.getElementById('root')
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
